
  <div class="ibox float-e-margins">
    <div class="ibox-title" style="border: none;">
      <h5 style="color: #00afef"><i>Add</i></h5>
        <div class="ibox-tools add">
          <button id="save_asset" class="btn btn-primary btn-sm save-btn-tab" type="">Save</button>
          <button id="cancel_add" class="btn-outline btn btn-primary btn-sm add-cancel cancel-btn-tab" >Cancel</button>
        </div>
    </div>
    <div class="ibox-content">
      <form class="form-horizontal" id="assetForm">
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Asset Code</label>
            <div class="col-lg-8">
                <input readonly name="assetCode" type="text" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Main Category</label>
            <div class="col-lg-8">
                <select name="mainCat" class="form-control">
                    <option value="">--</option>
                    <?php foreach($main_category as $mc): ?>
                    <option value="<?php echo $mc->category_code; ?>"><?php echo $mc->description; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Sub Category</label>
            <div class="col-lg-8">
                <select name="subCat" class="form-control">
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Asset Name</label>
            <div class="col-lg-8">
                <input type="text" name="assetName" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Department</label>
            <div class="col-lg-8">
                <select name="department" class="form-control">
                    <option value="">--</option>
                    <?php foreach($dep as $s): ?>
                    <option value="<?php echo $s->lookup_code; ?>"><?php echo $s->meaning; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Location</label>
            <div class="col-lg-8">
                <select name="location" class="form-control">
                    <option value="">--</option>
                    <?php foreach($location as $mc): ?>
                    <option value="<?php echo $mc->location_code; ?>"><?php echo $mc->description; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Purchase Date</label>
            <div class="col-lg-8">
                <input type="date" name="purchaseDate" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Asset Description</label>
            <div class="col-lg-8">
                <input type="text" name="assetDescription" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Brand Name</label>
            <div class="col-lg-8">
                <input type="text" name="brandName" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Model</label>
            <div class="col-lg-8">
                <input type="text" name="model" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Asset Serial Number</label>
            <div class="col-lg-8">
                <input type="text" name="assetSerNum" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Asset Currency</label>
            <div class="col-lg-8">
                <select name="assetCurrency" class="form-control">
                    <option value="">--</option>
                    <?php foreach($asset_currency as $mc): ?>
                    <option value="<?php echo $mc->lookup_code; ?>" <?php echo $mc->lookup_code == 'PHP' ? 'selected' : ''; ?>><?php echo $mc->meaning; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Asset Cost</label>
            <div class="col-lg-8">
                <input type="number" name="assetCost" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Depreciation Years</label>
            <div class="col-lg-8">
                <input type="text" name="dep_yrs" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Salvage Value</label>
            <div class="col-lg-8">
                <input type="number" name="salvageValue" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Book Value</label>
            <div class="col-lg-8">
                <input type="text" readonly name="bookValue" class="form-control">
            </div>
        </div>
        <div class="form-group" style="display: none">
            <label style="text-align: right;" class="col-lg-3 control-label">Depreciation Value</label>
            <div class="col-lg-8">
                <input type="text" name="depValue" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Contract Vendor</label>
            <div class="col-lg-8">
                <input type="text" name="contractVendor" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Asset Status</label>
            <div class="col-lg-8">
                 <select name="assetStatus" class="form-control">
                    <option value="">--</option>
                    <?php foreach($asset_status as $mc): ?>
                    <option value="<?php echo $mc->lookup_code; ?>" <?php echo $mc->lookup_code == 'ACT' ? 'selected' : ''; ?>><?php echo $mc->meaning; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Asset Owner</label>
            <div class="col-lg-8">
                <select name="assetOwner" class="form-control">
                    <option value="">--</option>
                    <?php foreach($employee as $mc): ?>
                    <option value="<?php echo $mc->employee_id; ?>"><?php echo $mc->first_name.' '.$mc->last_name; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Depreciation Cycle</label>
            <div class="col-lg-8">
                <select name="depCycle" class="form-control">
                    <option value="">--</option>
                    <?php foreach($depre_cycle as $mc): ?>
                    <option value="<?php echo $mc->lookup_code; ?>"><?php echo $mc->meaning; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
         <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Last Depreciation Run</label>
            <div class="col-lg-8">
                <input type="date" readonly name="depLastRun" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label style="text-align: right;" class="col-lg-3 control-label">Retirement Date</label>
            <div class="col-lg-8">
                <input type="text" readonly name="retirementDate" class="form-control">
            </div>
        </div>
      </div>
      </form>
    </div>
  </div>

  <script type="text/javascript">
  $(document).ready(function() {
    $('#assetForm').data('serialize',$('#assetForm').serialize());

    var salvage_value = $('input[name=salvageValue]');
    var asset_cost = $('input[name=assetCost]');
    var salvage_value = $('input[name=salvageValue]');
    var purchase_date = $('input[name=purchaseDate]');
    var depre_yrs = $('input[name=dep_yrs]');
    var book_value = $('input[name=bookValue]');
    var retirement_date = $('input[name=retirementDate]');
    var diff = 0;
    var sum = 0;

    $('select').chosen({width: '100%'});
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green'
    });

    $(document).on('keyup','input[name=purchaseDate]',function(){
      purchase_date_f($(this).val());
    });

    $(document).on('keyup','input[name=dep_yrs]',function(){
      depre_yrs_f($(this).val());
    });

    $(document).on('blur','input[name=dep_yrs]',function(){
      depre_yrs_f($(this).val());
    });

    $(document).on('blur','input[name=purchaseDate]',function(){
      purchase_date_f($(this).val());
    });
    /* ==================================================================== */
    $(document).on('keyup','input[name=assetCost]',function(){
      asset_cost_f($(this).val());
    });

    $(document).on('keyup','input[name=salvageValue]',function(){
      asset_cost_f($(this).val());
    });

    $(document).on('blur','input[name=salvageValue]',function(){
      salvage_value_f($(this).val());
    });

    $(document).on('blur','input[name=assetCost]',function(){
      salvage_value_f($(this).val());
    });

    function asset_cost_f(val){
       if(val !== '' && salvage_value.val()){
        diff = parseFloat(val) - parseFloat(salvage_value.val());
        book_value.val(diff);
      }
    }

    function salvage_value_f(val){
      if(val !== '' && asset_cost.val()){
        diff = parseFloat(asset_cost.val()) - parseFloat(val);
        book_value.val(diff);
      }
    }

    function purchase_date_f(val){
       if(val !== '' && depre_yrs.val()){
        var date = moment(val);
        sum = date.add(depre_yrs.val(), 'y').format("MMMM DD, YYYY");
        retirement_date.val(sum);
      }
    }

    function depre_yrs_f(val){
      if(val !== '' && purchase_date.val()){
        var date = moment(purchase_date.val());
        sum = date.add(val, 'y').format("MMMM DD, YYYY");
        retirement_date.val(sum);
      }
    }


    $(document).on('change','#assetForm select[name=mainCat]',function(){
      $.ajax({
        url: "<?php echo base_url($this->session->userdata('forajax') . '/getSubCat'); ?>",
        method: "POST",
        data: { 'cat_code' : $(this).val() },
        dataType: 'json',
        success: function(data){
          var tmp = '';
          $.each(data,function(i, item) {
            tmp += '<option value="'+data[i].sub_category_code +'">'+ data[i].description +'</option>';
          });
          $('#assetForm select[name=subCat]').html(tmp);
          $("select").trigger("chosen:updated");
        }
      });
    });

        $(document).on('click','#save_asset',function(){
            if($("#assetForm" ).valid() == true){
                $.ajax({
                  url:"<?php echo base_url($this->session->userdata('forajax') . '/insert'); ?>",
                  method:'post',
                  data:$('#assetForm').serialize(),
                  dataType:'json',
                  success: function (data) {
                      if(data.err == 1){
                        notifymsg('danger',data.msg);
                      }else{
                        $('#add-link').remove();
                        $('#add').remove();
                        $('#tab-list-2 a:first').tab('show');
                        $('#count_add').val('0');
                        notifymsg('success','<b>Success</b>: Data Saved.');
                      }

                  },
                  beforeSend: function(){

                  }
              });
            }
        });

        function alert_cancel(){
                if($('#assetForm').serialize()!=$('#assetForm').data('serialize')){
                // Form has changed!!!
                swal({
                  title: "Are you sure?",
                  text: "Your work will not be save",
                  type: "warning",
                  showCancelButton: true,
                  confirmButtonColor: "#DD6B55",
                  confirmButtonText: "Leave",
                  cancelButtonText: "Stay",
                  closeOnConfirm: true,
                  closeOnCancel: true },
              function (isConfirm) {
                  if (isConfirm) {
                    swal("success");
                      close_add(); active_search();
                      notifyClass("clear","");
                  } else {
                      swal("Cancelled");
                  }
              });
                }else{
                    close_add(); active_search();
                    notifymsg("hide","hide");
                }
            }
        $('#cancel_add').click(function(){
            alert_cancel();
        });

        $('#addX').click(function(){
            alert_cancel();
        });

        $("#assetForm").validate({
                 rules: {
                      mainCat: {
                        required: true
                      },
                      subCat: {
                        required: true
                      },
                      department: {
                        required: true
                      },
                      assetName: {
                        required: true
                      },
                      location: {
                        required: true
                      },
                      purchaseDate: {
                        required: true
                      },
                      assetDescription: {
                        required: true
                      },
                      assetCurrency: {
                        required: true
                      },
                      dep_yrs: {
                        required: true
                      },
                      salvageValue: {
                        required: true
                      },
                      salvageValue: {
                        required: true
                      },
                      depValue: {
                        required: true
                      },
                      depCycle: {
                        required: true
                      },
                      status: {
                        required: true
                      },
                      assetCost: {
                        required: true
                      }
                 }
        });
  });
  </script>